/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *Javascript document
 **/

var seats = null;
var types = null;

function loadInformation(){
    loadSource();
    loadFinal();
}
function loadSource(){
    var argsArray=new Array();
    argsArray.push(newArg("Action","Source"));
    sendRequest("../controller/controllerTickets.jsp",argsArray,showSource);
}

function loadFinal(){
    var argsArray=new Array();
    argsArray.push(newArg("Action","Final"));
    sendRequest("../controller/controllerTickets.jsp",argsArray,showFinal);
}

function showSource(){
    if (isValidResponse(this))
    {
        var res=this.responseText;
        if(res!="")
        {
            var info=eval("("+res+")");
            if(info.errorcode == 0){
                if(info.data.length<1){
                    jConfirm('No existen ciudades origen disponibles aun', 'Error', 'Error Dialog');
                }else{
                    var selS = document.getElementById("selSource");
                    var selectS = "";
                    for(var i=0; i<info.data.length; i++){
                        selectS+='<option>';
                        selectS+=info.data[i].o+'</option>';
                    }
                    selS.innerHTML = selectS;
                }
            }else
                window.alert("Error");
        }
    }

}

function showFinal(){
    if (isValidResponse(this))
    {
        var res=this.responseText;
        if(res!="")
        {
            var info=eval("("+res+")");
            if(info.errorcode == 0)
            {
                if(info.data.length<1){
                    jConfirm('No existen ciudades destino disponibles aun', 'Error', 'Error Dialog');
                }else{
                    var selF = document.getElementById("selFinal");
                    var selectF = "";
                    for(var i=0; i<info.data.length; i++){
                        selectF+='<option>';
                        selectF+=info.data[i].d+'</option>';
                    }
                    selF.innerHTML = selectF;
                }

            }else
                window.alert("Error");
        }
    }

}

function findTravel(){

    var idSource = document.getElementById("selSource").value;
    var idFinal = document.getElementById("selFinal").value;
    var date = document.getElementById("txtDate").value;
    var hour = document.getElementById("txtHour").value;

    if(validateDate(date)){
        if(idSource.toString()=="" || idFinal.toString() == "" || hour=="" || date==""){
            jConfirm('Se deben diligenciar todos los campos', 'Error', 'Error Dialog');
        }else{
            var argsArray=new Array();
            argsArray.push(newArg("Action","find"));
            argsArray.push(newArg("cityS", idSource.toString()));
            argsArray.push(newArg("cityF", idFinal.toString()));
            argsArray.push(newArg("hour", hour));
            argsArray.push(newArg("date", date));
            argsArray.join("&");
            sendRequest("../controller/controllerTickets.jsp",argsArray,showFind);
        }
    }else{
        jConfirm('Fecha anterior al d&iacute;a de hoy. Por favor seleccione otra', 'Error', 'Error Dialog');
    }


}

function showFind(){
    if (isValidResponse(this))
    {
        var res=this.responseText;
        if(res!="")
        {
            var info=eval("("+res+")");

            if(info.errorcode == 0)
            {
                seats = new Array();
                types = new Array();
                for(var i=0; i<info.data.length; i++){
                    seats[i] = info.data[i].position;
                    types[i] = info.data[i].type;

                    if(seats[i]<10){
                        seats[i]="0"+seats[i];
                    }

                    if(types[i]=="Reservado"){
                        document.getElementById(seats[i]).className="boxStyleReserved";
                    }else if(types[i]=="Vendido"){
                        document.getElementById(seats[i]).className="boxStyleSold";
                    }
                }
                document.getElementById("form").style.display = "none";
                document.getElementById("formR").style.display = "block";

            }else
                jConfirm('No existen viajes programados', 'Error', 'Error Dialog');
        }else{
            jConfirm('Error en la base de datos', 'Error', 'Error Dialog');

        }
    }
}

function viewNumber(number){
    if(number<10){
        number = "0"+number;
    }
    if(document.getElementById(number).className!="boxStyleSold" && document.getElementById(number).className!="boxStyleReserved" )
        document.getElementById("txtPlace").value=number;
    else
        jConfirm('El puesto elegido se encuentra ocupado, Seleccione otro por favor', 'Error', 'Error Dialog');
}